package com.example.project.dao.impl;

import com.example.project.dao.IOffdutyDao;
import com.example.project.entity.Leaves;
import com.example.project.entity.Offduty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class OffdutyDaoImpl implements IOffdutyDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //实现销假添加方法
    @Override
    public int add(Offduty offduty) {
        return jdbcTemplate.update("insert into offduty(OffdutyId,leavesId,TripId,EmployeeId,EndDate,OffDutyDate)"+ "values (?,?,?,?,?,?)",
                offduty.getOffDutyId(),
                offduty.getLeavesId(),
                offduty.getTripId(),
                offduty.getEmployeeId(),
                offduty.getEndDate(),
                offduty.getOffDutyDate()
        );
    }

    //实现查询所有销假数据的方法
    @Override
    public List<Offduty> getAllOffduty(Offduty offduty) {
        return jdbcTemplate.query("select OffdutyId,leavesId,TripId,EmployeeId,EndDate,OffDutyDate from offduty",new Object[]{},
                new BeanPropertyRowMapper<>(Offduty.class));
    }

    //删除销假数据
    @Override
    public int deleteOffduty(Offduty offduty) {
        return jdbcTemplate.update("delete from offduty where OffdutyId=?",
                offduty.getOffDutyId());
    }

    //实现销假信息的更新
    @Override
    public int updateOffduty(Offduty offduty) {
        return jdbcTemplate.update("update offduty set"+" leavesId=?,TripId=?,EmployeeId=?,EndDate=?,OffDutyDate=? where OffdutyId=?",
                offduty.getLeavesId(),
                offduty.getTripId(),
                offduty.getEmployeeId(),
                offduty.getEndDate(),
                offduty.getOffDutyDate(),
                offduty.getOffDutyId()
                );
    }

    //实现单挑查询销假数据方法
    @Override
    public Offduty getOffduty(Offduty offduty) {
        List<Offduty> result =jdbcTemplate.query("select"+"`OffdutyId`,`leavesId`,`TripId`,`EmployeeId`,`EndDate`,`OffDutyDate`"+"from offduty where OffdutyId='"+offduty.getOffDutyId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Offduty.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
